package com.onehouse.trigger.http;

import com.onehouse.api.dto.RoomDTO;
import com.onehouse.api.dto.RoomStatusDTO;
import com.onehouse.domain.room.model.RoomEntity;
import com.onehouse.domain.room.model.RoomStatus;
import com.onehouse.domain.room.service.IRoomService;
import com.onehouse.types.constants.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin("*")
@RequestMapping("/room")
public class RoomController {
    
    @Autowired
    private IRoomService roomService;
    
    private RoomDTO convertToDTO(RoomEntity entity) {
        RoomDTO dto = new RoomDTO();
        dto.setRoomId(entity.getRoomId());
        dto.setRoomTypeId(entity.getRoomTypeId());
        dto.setRoomNumber(entity.getRoomNumber());
        dto.setStatus(RoomStatusDTO.valueOf(entity.getStatus().toString()));
        dto.setIsDeleted(entity.getIsDeleted());
        return dto;
    }

    private RoomEntity convertToEntity(RoomDTO dto) {
        return RoomEntity.builder()
                .roomId(dto.getRoomId())
                .roomTypeId(dto.getRoomTypeId())
                .roomNumber(dto.getRoomNumber())
                .status(RoomStatus.valueOf(dto.getStatus().toString()))
                .isDeleted(dto.getIsDeleted())
                .build();
    }

    @Operation(summary = "列出所有房间", description = "房间状态包括：" +
            "AVALIABLE," +
            "BOOKED," +
            "CHECKED_IN," +
            "CHECKED_OUT," +
            "CLEANING," +
            "MAINTENANCE," +
            "VACANT”")
    @GetMapping("/list")
    public AjaxResult listAll() {
        return AjaxResult.success(
            roomService.listAllRoom().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList())
        );
    }

    @Operation(summary = "批量创建房间", description = "传入参数包括：房间类型ID和房间号列表")
    @PostMapping("/create")
    public AjaxResult createRooms(@RequestParam(name = "RoomTypeId") Long typeId, @RequestBody List<String> roomNumbers) {
        try {
            roomService.createRooms(typeId, roomNumbers);
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "更新单个房间信息")
    @PutMapping("/update")
    public AjaxResult updateRoom(@RequestBody RoomDTO roomDTO) {
        try {
            roomService.updateRoom(convertToEntity(roomDTO));
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "批量更新房间信息")
    @PutMapping("/update/batch")
    public AjaxResult updateRooms(@RequestBody List<RoomDTO> roomDTOs) {
        try {
            roomService.updateRooms(
                roomDTOs.stream()
                    .map(this::convertToEntity)
                    .collect(Collectors.toList())
            );
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "按类型删除房间")
    @DeleteMapping("/delete/type/{typeId}")
    public AjaxResult deleteRoomsByType(@PathVariable Long typeId) {
        try {
            roomService.deleteRoomsByType(typeId);
            return AjaxResult.success();
        } catch (IllegalArgumentException | IllegalStateException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "根据房间号批量删除房间")
    @DeleteMapping("/delete/batch")
    public AjaxResult deleteRooms(@RequestBody List<String> roomNubers) {
        try {
            roomService.deleteRooms(roomNubers);
            return AjaxResult.success();
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
